package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.AboutInfo;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.AboutInfoDao;
import com.wcfsy.wechat.wx.util.FileUploadUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/about")
public class AboutInfoController {
	@Resource
	private AboutInfoDao aboutInfoDao;
	@Resource
	private FileUploadUtil fileUploadUtil;

	@RequestMapping({ "/view/page" })
	public String page() {
		return "about/about_list";
	}

	@RequestMapping({ "/view" })
	@ResponseBody
	public IPage<AboutInfo> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<AboutInfo>(aboutInfoDao.findByParam(paramMap),
				aboutInfoDao.count(paramMap), page, rows);
	}

	@RequestMapping({ "/add/page" })
	public String preAdd() {
		return "about/about_add";
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public JsonMessage add(AboutInfo info, HttpServletRequest request) {
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile("imgUrl");
			if (file != null && !file.isEmpty()) {
				String imgUrl = fileUploadUtil.upload(file);
				info.setImageUrl(imgUrl);
			}
			aboutInfoDao.add(info);
			return JsonMessage.getSuccessJson("添加成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("添加失败");
		}
	}

	@RequestMapping({ "/delete" })
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			aboutInfoDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("删除失败");
		}

	}

	@RequestMapping({ "/modify/page" })
	public ModelAndView preModify(int id) {
		ModelAndView mav = new ModelAndView("about/about_modify");
		mav.addObject("data", aboutInfoDao.getById(id));
		return mav;
	}

	@RequestMapping({ "/modify" })
	@ResponseBody
	public JsonMessage modify(int id, int status, int aboutType, int showIndex,
			String description, HttpServletRequest request) {
		try {
			AboutInfo info = new AboutInfo();
			info.setUpdateDate(new Date());
			info.setId(id);
			info.setStatus(status);
			info.setAboutType(aboutType);
			info.setShowIndex(showIndex);
			info.setDescription(description);
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile("imgUrl");
			if (file != null && !file.isEmpty()) {
				String imgUrl = fileUploadUtil.upload(file);
				info.setImageUrl(imgUrl);
			}
			aboutInfoDao.update(info);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("修改失败");
		}
	}

	@RequestMapping({ "/add/view" })
	public ModelAndView preView(AboutInfo info) {
		ModelAndView mav = new ModelAndView("about/about_view");
		mav.addObject("data", info);
		return mav;
	}

}
